/**
 * 实体属性映射
 */
import tnx from '@/tnx.js';
import QueryingProperty from './QueryingProperty.js';
import ClassProperty from './ClassProperty.js';

export default class EntityPropertyMapping {

    id = '';
    name = '';
    type = '';
    length = 0;
    scale = 0;
    nullable = true;
    defaultValue = '';
    converter = '';
    /**
     * @type {QueryingProperty}
     */
    querying = null;
    /**
     * @type {ClassProperty}
     */
    command = null;

    static CONVERTER_AUTO = 'auto';

    static of(object) {
        if (typeof object !== 'object') {
            return undefined;
        }
        let mapping = new EntityPropertyMapping();
        mapping.update(object);
        return mapping;
    }

    update(object) {
        tnx.util.object.rewriteSimpleDefined(this, object);
        if (typeof object === 'object') {
            if (object.querying) {
                this.querying = QueryingProperty.of(object.querying);
            }
            if (object.command) {
                this.command = ClassProperty.of(object.command);
            }
        }
        return this;
    }

    clone() {
        return EntityPropertyMapping.of(this);
    }

}
